
from access.core.codesnippet import CodeSnippet
from third_party.active_state.recipes.autoassign import autoassign
import django.template

class CodeSnippetGenerator:
  @autoassign
  def __init__(self,
               rendering_context = {}): pass

  def Generate(self, pattern):
    if not pattern.context \
    or not len(pattern.context):
      return [CodeSnippet(pattern.ID_pattern,
                          pattern.value_pattern)]
    
    #TODO see if we need extra validation for context / anything else.
    snippets = [] #HACK Fix this later. Basically need to calculate combinations.

    for key in pattern.context:
      for value in pattern.context[key]:
        self.rendering_context.update({key: value})
        context = django.template.Context(self.rendering_context)
        ID = django.template.Template(pattern.ID_pattern).render(context)
        value = django.template.Template(pattern.value_pattern).render(context)

        snippets.append(CodeSnippet(ID, value))
    return snippets
